/**
 * Created by jackliu on 15/12/6.
 */
import SJGPage = require('./../kernel/cont/SJGPage');
import SJGReplaceGroup = require('./../kernel/replace/SJGReplaceGroup');
import SJGTypeSetTitleGroup = require('./SJGTypeSetTitleGroup');
import SJGTypeSetTextGroup = require('./SJGTypeSetTextGroup');
import SJGTypeSetTextGroup2 = require('./SJGTypeSetTextGroup2');
import SJGTypeSetImageGroup = require('./SJGTypeSetImageGroup');
import SJGTypeSetImageTextGroup = require('./SJGTypeSetImageTextGroup');
import SJGTypeSetImageTextGroup2 = require('./SJGTypeSetImageTextGroup2');
import SJGTypeSetChartGroup = require('./SJGTypeSetChartGroup');
import SJGTypeSetCommentGroup = require('./SJGTypeSetCommentGroup');

var async  = require( 'async'); // 同步控制
export = SJGTypeSet;

class SJGTypeSet{
    public page:SJGPage = null;

    // 排版核心
    // 参数：p页面
    // 参数：callback 为async的回调函数，例如：asyncCallback(null, 'one');
    public do(p:SJGPage,callback){
        var THIS = this;
        this.page = p;

        var asyncFuncs = [];
        for (var replace in this.page.userReplaces) {
            asyncFuncs.push((function (op:SJGReplaceGroup) {
                return function (asyncCallback) {  // asyncCallback(null, 'one');// asyncCallback(null, 'two');
                    THIS.doGroup(op, asyncCallback);
                }
            })(this.page.userReplaces[replace])); // 利用闭包解决动态参数传递问题
        }
        async.parallel(asyncFuncs, function (err, results) {
            // 处理自动替换图标
            p.getAutoReplaceGraph(function(){
                p.getAutoReplaceGraphInTextHead(function(){
                    callback(null, null);
                });
            });
        });

    }
    // 针对分组排版
    // 参数：group可替换分组
    // 参数：callback 为async的回调函数，例如：asyncCallback(null, 'one');
    public doGroup(group:SJGReplaceGroup, callback){


        // 1titleGroup/2textGroup/3imageGroup/4graphGroup/5tableGroup/6imageTextGroup/
        switch(group.kind){
            case SJGReplaceGroup.TITLEGROUP:// 页标题／副标题
                var tsTitle:SJGTypeSetTitleGroup = new SJGTypeSetTitleGroup();
                tsTitle.do(this.page, group, callback);
                break;
            case SJGReplaceGroup.TEXTGROUP:// 旧文字 [待废弃]
                var tsText:SJGTypeSetTextGroup = new SJGTypeSetTextGroup();
                tsText.do(this.page, group, callback);
                break;
            case SJGReplaceGroup.IMAGEGROUP://  图片组
                var tsImage:SJGTypeSetImageGroup = new SJGTypeSetImageGroup();
                tsImage.do(this.page, group, callback);
                break;
            case SJGReplaceGroup.IMAGETEXTGROUP:// 旧图文 [待废弃]
                var tsImageText:SJGTypeSetImageTextGroup = new SJGTypeSetImageTextGroup();
                tsImageText.do(this.page, group, callback);
                break;
            case SJGReplaceGroup.CHARTGROUP://  图表组
                var tsChart:SJGTypeSetChartGroup = new SJGTypeSetChartGroup();
                tsChart.do(this.page, group, callback);
                break;
            case SJGReplaceGroup.TEXTGROUP2:// 新文字或者旧文字
                var tsText2:SJGTypeSetTextGroup2 = new SJGTypeSetTextGroup2();
                tsText2.do(this.page, group, callback);
                break;
            case SJGReplaceGroup.IMAGETEXTGROUP2:// 新图文或者旧图文
                var tsImageText2:SJGTypeSetImageTextGroup2 = new SJGTypeSetImageTextGroup2();
                tsImageText2.do(this.page, group, callback);
                break;
            case SJGReplaceGroup.COMMENTGROUP:// 总结组
                var tsComment:SJGTypeSetCommentGroup = new SJGTypeSetCommentGroup();
                tsComment.do(this.page, group, callback);
                break;
            default :
                callback(null, null);
                break;
        }
    }


}
